package com.ljsw.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ljsw.adapter.PKMoneyInfoAdapter;
import com.ljsw.application.App;
import com.ljsw.dialog.TimeOut;
import com.ljsw.entity.gpk.StockLive;
import com.ljsw.server.UpServer;
import com.ljsw.util.DialogManager;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;

import static com.ljsw.application.App.app;
import static com.ljsw.application.App.user;


/****
 * 盘库明细信息 查看信息
 */
public class PKMoenyInfoActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "PKListInfoActivity";
    private Handler handler;// 声明一个hander
    private ListView pkmoney_info;///
    private PKMoneyInfoAdapter PKMoneyInfoAdapter;

    private List<StockLive> StockLivelist = new ArrayList<StockLive>();// 数据源
    private String PKNo;
    private String PKNetResult;// 返回的字符传结果
    private DialogManager dmanager;//弹出框管理类
    private TimeOut timeout;
    private String Userid;
    private Button moneyinfo_update;//  更新
    private ImageView moneyinfoblack;//返回


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pkmoeny_info);
        dmanager = new DialogManager(PKMoenyInfoActivity.this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        timeout = dmanager.getTimeOut();//超时弹框
        Intent intent = new Intent();
        InitView();
        PKNo = getIntent().getStringExtra("TaskNo");
        Userid = App.userZhanghao;
        LoadData();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0://登录成功后跳转 上门服务页面
                        app.user = user;

                        if(StockLivelist.size()>0){

                            PKMoneyInfoAdapter = new PKMoneyInfoAdapter(StockLivelist, PKMoenyInfoActivity.this);
                            pkmoney_info.setAdapter(PKMoneyInfoAdapter);

                        }else{

                        }
                        PKMoneyInfoAdapter.notifyDataSetChanged();
                        Log.d("login用户role------------", user.getUserId());
                        break;
                    case 1://登录超时
                        Toast.makeText(PKMoenyInfoActivity.this, "正在登陆请检查网络", Toast.LENGTH_SHORT).show();
//                        timeout.show();
//                        queding();
                        break;
                    case 2://异常加载，无法访问服务器
                        Toast.makeText(PKMoenyInfoActivity.this, "请求数据失败", Toast.LENGTH_SHORT).show();
//                        timeout.show();
//                        timeout.setTitle("加载异常");
//                        queding();
                        break;

//                    case 3://异常加载，无法访问服务器
//                        Toast.makeText(PKMoenyInfoActivity.this, "请求超时", Toast.LENGTH_SHORT).show();
////                        timeout.show();
////                        timeout.setTitle("加载异常");
////                        queding();
//                        break;
                    case 3://异常加载，无法访问服务器
                        Toast.makeText(PKMoenyInfoActivity.this, "请求超时", Toast.LENGTH_SHORT).show();
//                        timeout.show();
//                        timeout.setTitle("加载异常");
//                        queding();
                        break;
                    case 20:
                        PKMoneyInfoAdapter.notifyDataSetChanged();
                        break;

                    default:
                        Toast.makeText(PKMoenyInfoActivity.this, "请检查网络服务", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    }

    /****
     * 再次网络请求
     */
    private void queding() {
        LoadData();
    }

    /****
     * 数据加载
     */
    private void LoadData() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    if (null == PKNo || PKNo.equals("")) {
                        handler.sendEmptyMessage(4);//查无此人信息
                    } else {
                        Log.e(TAG, PKNo);
                        Log.e(TAG, Userid);
                        PKNetResult = UpServer.listCoreDetail(PKNo, Userid);
                        Log.e(TAG, "-----" + PKNetResult);
                        if (null == PKNetResult) {
                            handler.sendEmptyMessage(2);//查无此人信息
                        } else {
                            Gson gson = new Gson();
                            StockLive[] mTailZerotoEntity = gson.fromJson(PKNetResult, StockLive[].class);
                            StockLivelist.clear();//  清空
//                            for (int i = 0; i < mTailZerotoEntity.length; i++) {
//                                StockLivelist = Arrays.asList(mTailZerotoEntity);
//                            }

                            List arrList = Arrays.asList(mTailZerotoEntity);
                            StockLivelist = new ArrayList(arrList);

                            if (StockLivelist != null || StockLivelist.equals("")) {
                                handler.sendEmptyMessage(0);//验证成功 返回三个元素在user

                            } else {
                                handler.sendEmptyMessage(2);//查无此人信息
                            }
                        }
                    }
                } catch (SocketTimeoutException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(3);//超时链接
                } catch (TimeoutException e) {
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//不是数字
                } catch (Exception e) {

                    e.printStackTrace();
                    Log.e("b", "异常：：：" + e);
                    handler.sendEmptyMessage(2);
                }

            }
        }).start();

    }

    private void InitView() {

        pkmoney_info = (ListView) findViewById(R.id.pkmoney_info);
        moneyinfo_update = (Button) findViewById(R.id.moneyinfo_update);
        moneyinfo_update.setOnClickListener(this);
        moneyinfoblack = (ImageView) findViewById(R.id.moneyinfoblack);
        moneyinfoblack.setOnClickListener(this);
//        PKMoneyInfoAdapter = new PKMoneyInfoAdapter(PKPKMoenyInfolist, PKMoenyInfoActivity.this);
//        pkmoney_info.setAdapter(PKMoneyInfoAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.moneyinfo_update:
                LoadData();
                handler.sendEmptyMessage(20);
                break;
            case R.id.moneyinfoblack:
                PKMoenyInfoActivity.this.finish();
                break;


        }
    }
}
